package com.changgou.goods.service.impl;

import com.changgou.goods.dao.BrandMapper;
import com.changgou.goods.pojo.Brand;
import com.changgou.goods.service.BrandService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.annotation.Transient;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @author caixiaofeng
 * @company xxx公司科技有限公司
 * @create 2020-04-28 11:22
 */
@Service
public class BrandServiceImpl implements BrandService {
    @Resource
    private BrandMapper brandMapper;


    @Override
    public List<Brand> findAll() {
        List<Brand> brandList = brandMapper.selectAll();
        return brandList;
    }

    @Override
    public Brand findById(Integer id) {
        Brand brand = brandMapper.selectByPrimaryKey(id);
        return brand;
    }

    @Override
    public void add(Brand brad) {
        brandMapper.insertSelective(brad);
    }

    @Override
    public void update(Brand brand) {
        brandMapper.updateByPrimaryKeySelective(brand);
    }

    @Override
    public void delete(Integer id) {
        brandMapper.deleteByPrimaryKey(id);
    }

    @Override
    public List<Brand> findList(Map<String, Object> searchMap) {
        Example example = new Example(Brand.class);
        Example.Criteria criteria = example.createCriteria();
        if(searchMap!=null){
            String name = (String) searchMap.get("name");

            if(name!=null && !"".equals(name)){
                criteria.andLike("name","%"+name+"%");
            }

            String letter = (String) searchMap.get("letter");
            if(letter!=null && !"".equals(letter)){
                criteria.andLike("letter",letter);
            }
            return brandMapper.selectByExample(example);
        }
        return null;
    }

    @Override
    public Page<Brand> findPage(Integer page, Integer size) {
        PageHelper.startPage(page,size);
        return (Page<Brand>) brandMapper.selectAll();
    }

    @Override
    public Page<Brand> findPage(Map<String, Object> searchMap, Integer page, Integer size) {
        PageHelper.startPage(page,size);
        Example example = new Example(Brand.class);
        Example.Criteria criteria = example.createCriteria();
        if(searchMap!=null){
            String name = (String) searchMap.get("name");
            if(name!=null && !"".equals(name)){
                criteria.andLike("name","%"+name+"%");
            }

            String letter = (String) searchMap.get("letter");
            if(letter!=null && !"".equals(letter)){
                criteria.andLike("letter",letter);
            }
            return (Page<Brand>) brandMapper.selectByExample(example);
        }
        return null;
    }

    @Override
    public List<Map> findListByCategoryName(String categoryName) {
        List<Map> listByCategoryName = brandMapper.findListByCategoryName(categoryName);
        return listByCategoryName;
    }
}
